package com.app.xinfadi.ui.home.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.app.xinfadi.R;
import com.app.xinfadi.base.Constant;
import com.app.xinfadi.util.LogUtil;
import com.app.xinfadi.widget.image_browser.ImageExploreActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片列表
 */
public class GridViewImageNomalAdapter extends BaseAdapter {
    private final DisplayImageOptions options;
    private Context context;
    private List<String> imageUrl;
    private ImageSize mImageSize;
    private String TAG = "GridViewImageAdapter";

    public GridViewImageNomalAdapter(Context context, List<String> imageUrl) {
        this.context = context;
        this.imageUrl = imageUrl;
        mImageSize = new ImageSize(250, 250);
        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.image_failed) // image连接地址为空时
                .showImageOnFail(R.drawable.image_failed) // image加载失败
                .showImageOnLoading(R.drawable.image_failed)
                .cacheInMemory(true) // 加载图片时会在内存中加载缓存
                .cacheOnDisk(true)
                .build();
    }

    @Override
    public int getCount() {
        return imageUrl.size();
    }

    @Override
    public Object getItem(int position) {
        return imageUrl.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        // 正常列表
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.common_image_list_item, null);
            holder = new ViewHolder();
            holder.common_iamge = (ImageView) convertView.findViewById(R.id.common_iamge);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ImageView image = holder.common_iamge;
        if (imageUrl != null && !TextUtils.isEmpty(imageUrl.get(position))) {
            LogUtil.e(TAG, Constant.IMAGE_URL + imageUrl.get(position));
            ImageLoader.getInstance().loadImage(Constant.IMAGE_URL + imageUrl.get(position), mImageSize, options,
                    new SimpleImageLoadingListener() {
                        @Override
                        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                            super.onLoadingComplete(imageUri, view, loadedImage);
                            image.setImageBitmap(loadedImage);
                        }
                    });
        }
        return convertView;
    }

    class ViewHolder {
        ImageView common_iamge;
    }

}

// public class BottomPopupWindow extends PopupWindow {
// private View drop_downlist_popupwindow;
// private Context context;
//
// public BottomPopupWindow(Context context, int width, int height) {
// this.context = context;
// drop_downlist_popupwindow =
// LayoutInflater.from(this.context).inflate(R.layout.myquestion_bottom_pop_view,
// null);
// this.setContentView(drop_downlist_popupwindow);
// this.setWidth(width);
// this.setHeight(height);
// this.setFocusable(true);
// this.setOutsideTouchable(true);
// // this.update();
// ColorDrawable dw = new ColorDrawable(0000000000);
// // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
// this.setBackgroundDrawable(dw);
// }
// }
